package com.ly.upload;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan("com.ly.upload")
// DispatcherServlet 是SpringMVC标志性的类
@ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnProperty(prefix = "ly.upload", name = "enable")
public class UploadConfig implements WebMvcConfigurer {

    @Value("${ly.upload.upload-dir}")
    private String uploadDir;
    @Value("${ly.upload.web-path}")
    private String webPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(webPath + "/**")
                .addResourceLocations("file:/" + uploadDir + "/");
    }
}
